package web.java.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import web.java.dao.EmployeeDao;
import web.java.entity.Employee;

public class EmpDaoImpl implements EmployeeDao{

	@Override
	public List<Employee> getAll() {
		//连接数据库
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		List<Employee> list=new ArrayList<Employee>();
		Connection conn=null;
		try {
			conn=dataSource.getConnection();
			String sql="select * from emp";
			QueryRunner runner=new QueryRunner();
			list=runner.query(conn,sql,new BeanListHandler<Employee>(Employee.class));
			conn.close();
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean verify(String username, String password) {
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		Connection conn=null;
		String sql="SELECT * FROM USER WHERE NAME=? AND PASSWORD=?;";
		try{
			conn=dataSource.getConnection();
			QueryRunner runner=new QueryRunner();
			Employee emp=runner.query(conn,sql,new BeanHandler<Employee>(Employee.class),username,password);
			conn.close();
			if(emp!=null){
				return true;
			}else{
				return false;
			}
		}catch(SQLException e){
			throw new RuntimeException(e);
		}
	}
	public static void main(String[] args) {
		EmpDaoImpl edi=new EmpDaoImpl();
		/*List<Employee> list=edi.getAll();
		for(Employee emp:list){
			System.out.println(emp);
		}*/
		boolean b=edi.verify("cainter", "1");
		System.out.println(b);
	}
}
